package cn.xinyangtong.shopping.portal.controller;

import cn.xinyangtong.facade.pojo.CatResult;
import cn.xinyangtong.facade.pojo.Item;
import cn.xinyangtong.facade.rest.ItemCatService;
import cn.xinyangtong.facade.rest.ItemService;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Created by fengbo on 2017/7/18.
 */
@Controller
@RequestMapping("/item")
public class ItemController {

    @Autowired
    private ItemCatService itemCatService;
    @Autowired
    private ItemService itemService;

    @RequestMapping(value = "/itemcat/list", //
            produces= MediaType.APPLICATION_JSON_VALUE + ";charset=utf-8")
    @ResponseBody
    public String showItemCat(String callback) {
        CatResult catResult = itemCatService.getItemCatList();
        //把pojo转换成字符串
        String json = JSON.toJSONString(catResult);
        //拼装返回值
        String result = callback + "(" + json + ");";
        System.out.println(result);
        return result;
    }

    @RequestMapping("/{itemId}")
    public String showItem(@PathVariable Long itemId, Model model) {
        Item item = itemService.getItemBaseInfo(itemId);
        model.addAttribute("item", item);
        return "item";
    }

    @RequestMapping("/desc/{itemId}")
    @ResponseBody
    public String showItemDesc(@PathVariable Long itemId, String callback) {
        String result = itemService.getItemDesc(itemId);
        return callback + "(" + result + ")";
    }

    @RequestMapping("/param/{itemId}")
    @ResponseBody
    public String showItemParam(@PathVariable Long itemId) {
        return itemService.getItemParam(itemId);
    }

}
